﻿var InPageHtmlFixture = new function () {

    $.extend(this, BaseFixture);

    var dataKeys = {
        X: 'hdunv_x'
        , Y: 'hdunv_y'
        , Width: 'hdunv_w'
        , Height: 'hdunv_h'
        , Name: 'hdunv_name'
        , Type: 'hdunv_type'
        , Depth: 'hdunv_depth'
        , Speed: 'hdunv_speed'
        , Direction: 'hdunv_direction'
        , G: 'hdunv_g'
        , Mass: 'hdunv_mass'
        , IsStarFieldEnabled: 'starfield_enabled'
        , IsShootingStarEnabled: 'shootingstar_enabled'
    };

    this.GetFixture = function () {

        var jqFixtureRoot = $(".hdunv-fixture");
        if (jqFixtureRoot.length == 0)
            return BaseFixture.GetFixture();

        var universeJq = jqFixtureRoot.children("[data-hdunv_type=Universe]");
        if (universeJq.length == 0)
            return BaseFixture.GetFixture();
        var universeData = universeJq.data();
        var theUniverse = new Universe(ProcessSizeAndPosition(universeData), universeData[dataKeys.Name]);
        if (universeData[dataKeys.IsStarFieldEnabled] != null)
            theUniverse.ToggleStarfield(universeData[dataKeys.IsStarFieldEnabled]);
        if (universeData[dataKeys.IsShootingStarEnabled] != null)
            theUniverse.ToggleShootingStars(universeData[dataKeys.IsShootingStarEnabled]);
        theUniverse.AssociatedJqContent = universeJq.children(".hdunv-content").contents();

        //Galaxies
        universeJq.children("[data-hdunv_type=Galaxy]").each(function (index) {

            var galaxyData = $(this).data();
            var galaxy = new Galaxy(galaxyData[dataKeys.Depth], ProcessSizeAndPosition(galaxyData), galaxyData[dataKeys.Name]);
            galaxy.AssociatedJqContent = $(this).children(".hdunv-content").contents();
            theUniverse.AddChild(galaxy);

            //Solar Systems
            $(this).children("[data-hdunv_type=SolarSystem]").each(function (index) {

                var star = null;
                var starJq = $(this).children('[data-hdunv_type=Star]').first();
                if (starJq.length > 0) {
                    var starData = starJq.data();
                    var star = new Star(starData[dataKeys.G], starData[dataKeys.Mass], ProcessSizeAndPosition(starData), starData[dataKeys.Name]);
                    star.AssociatedJqContent = starJq.children(".hdunv-content").contents();
                }

                var solarSystemData = $(this).data();
                var solarSystem = new SolarSystem(solarSystemData[dataKeys.Speed], ProcessSpinDirection(solarSystemData[dataKeys.Direction]), star, ProcessSizeAndPosition(solarSystemData), solarSystemData[dataKeys.Name]);
                solarSystem.AssociatedJqContent = $(this).children(".hdunv-content").contents();
                galaxy.AddChild(solarSystem);

                //Planets
                ProcessPlanetsRecursively(solarSystem, $(this));
            });

        });

        return theUniverse;
    };

    //Helper Methods
    function ProcessSpinDirection(datasetDirection) {
        return datasetDirection.toLowerCase() == "cw" ? BasicPhysics.SPIN_DIRECTION_CLOCKWISE : BasicPhysics.SPIN_DIRECTION_COUNTER_CLOCKWISE;
    };

    function ProcessSizeAndPosition(dataset) {
        return new Rectangle(new Point(dataset[dataKeys.X], dataset[dataKeys.Y]), new Size(dataset[dataKeys.Width], dataset[dataKeys.Height]));
    };

    function ProcessPlanetsRecursively(parentEntity, parentJq) {
        var planetsJq = parentJq.children("[data-hdunv_type=Planet]");

        //Recursion stop condition
        if (planetsJq.length == 0)
            return;

        planetsJq.each(function (index) {
            var planetData = $(this).data();
            var planet = new Planet(planetData[dataKeys.Speed], ProcessSpinDirection(planetData[dataKeys.Direction]), planetData[dataKeys.G], planetData[dataKeys.Mass], ProcessSizeAndPosition(planetData), planetData[dataKeys.Name]);
            planet.AssociatedJqContent = $(this).children(".hdunv-content").contents();
            parentEntity.AddChild(planet);

            //Recursion here
            if ($(this).children().length > 0)
                ProcessPlanetsRecursively(planet, $(this));
        });

    };

};